var pluginsInfo = [
    {
        "pluginId":"0",
        "showName":"请选择"
    },
    {
        "pluginId":"audit",
        "groupName":"rad-audit",
        "showName":"是否插件",
        "pluginType":"radio"
    },
    {
        "pluginId":"gender",
        "groupName":"rad-gender",
        "showName":"性别插件",
        "pluginType":"radio"
    },
    {
        "pluginId":"stutype",
        "groupName":"chk-stutype",
        "showName":"学历插件",
        "pluginType":"checkbox"
    },
    {
        "pluginId":"treedbmap",
        "groupName":"tree-dbmap",
        "showName":"数据库映射插件",
        "pluginType":"tree",
        "needInit":true,
        "serviceType":"dbmap"
    }
];

var expendPlugins =
`
    <div id="audit" class="layui-form-item" style="display:none;">
        <label class="layui-form-label">是否：</label>
        <div class="layui-input-block">
            <input name="rad-audit" type="radio" value="1" lay-filter="radCellData" title="是">
            <input name="rad-audit" type="radio" value="2" lay-filter="radCellData" title="否">
        </div>
    </div>
    <div id="gender" class="layui-form-item" style="display:none;">
        <label class="layui-form-label">性别：</label>
        <div class="layui-input-block">
            <input name="rad-gender" type="radio" value="1" lay-filter="radCellData" title="男">
            <input name="rad-gender" type="radio" value="2" lay-filter="radCellData" title="女">
        </div>
    </div>
    <div id="stutype" class="layui-form-item" style="display:none;">
        <label class="layui-form-label">学历：</label>
        <div id="stutypeList" class="layui-input-block">
            <input type="checkbox" name="chk-stutype" value="1" lay-skin="primary" lay-filter="chkCellData" title="本科">
            <input type="checkbox" name="chk-stutype" value="2" lay-skin="primary" lay-filter="chkCellData" title="硕士">
            <input type="checkbox" name="chk-stutype" value="3" lay-skin="primary" lay-filter="chkCellData" title="博士">
            <input type="checkbox" name="chk-stutype" value="4" lay-skin="primary" lay-filter="chkCellData" title="高职">
            <input type="checkbox" name="chk-stutype" value="5" lay-skin="primary" lay-filter="chkCellData" title="专科">
        </div>
    </div>
    <div id="treedbmap" class="layui-form-item" style="display:none;">
        <div class="layui-form-item">
            <label class="layui-form-label">单元格映射</label>
            <div class="layui-input-block">
                <input name="tree-dbmap-type" type="radio" value="$" lay-filter="cellDbMapType" title="选中单元格">
                <input name="tree-dbmap-type" type="radio" value="#" lay-filter="cellDbMapType" title="整列单元格">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">是否必填项</label>
            <div class="layui-input-block">
                <input name="tree-dbmap-required" type="radio" value="*" lay-filter="cellDbMapRequired" title="是">
                <input name="tree-dbmap-required" type="radio" value="!" lay-filter="cellDbMapRequired" title="否">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">数据库映射</label>
            <div class="layui-input-block">
                <ul id="treeDbMapId" class="ztree"></ul>
            </div>
        </div>
    </div>
`;

layui.use(['form','upload','tree'], function () {
    let form = layui.form;
    let upload = layui.upload;
    let tree = layui.tree;
    //此处即为 radio 的监听事件
    form.on('radio(radCellData)', function(cell){
        let row_focus = Number(currEditCell.row_focus);
        let column_focus = Number(currEditCell.column_focus);
        let cellUpdate = {
            "v": cell.value, //内容的原始值
            "m": cell.elem.title, //内容的显示值
        };
        luckysheet.exitEditMode();
        luckysheet.setCellValue(row_focus,column_focus,cellUpdate);
    });
    form.on('checkbox(chkCellData)', function(cell){

        let row_focus = Number(currEditCell.row_focus);
        let column_focus = Number(currEditCell.column_focus);
        let v = "";
        let m = "";

        let currV = luckysheet.getCellValue(row_focus, column_focus);//当前实际值
        let currM = luckysheet.getCellValue(row_focus, column_focus, {type:"m"});//当前显示值

        let isChecked = cell.elem.checked;
        let chkVal = cell.value;
        let chkM = cell.elem.title;

        if(isChecked){
            if(currV!=undefined&&currV!=null&&currV.length>0){
                currV += "," + chkVal;
                currM += "," + chkM;
            }else{
                currV = chkVal;
                currM = chkM;
            }
            v = currV;
            m = currM;
        }else{
            let currVs = currV.split(',');
            let currMs = currM.split(',');

            for(var a=0;a<currVs.length;a++){
                if(currVs[a]==chkVal){

                }else{
                    v += "," + currVs[a];
                }
                if(currMs[a]==chkM){

                }else{
                    m += "," + currMs[a];
                }
            }
            if(v.length>0){
                v = v.substr(1);
                m = m.substr(1);
            }
        }
        let cellUpdate = {
            "v": v, //内容的原始值
            "m": m, //内容的显示值
        };
        luckysheet.exitEditMode();
        luckysheet.setCellValue(row_focus,column_focus,cellUpdate);
    });
    form.on('select(selCellData)', function(cell){
        var v = cell.value;
        if(v!=undefined&&v!=null&&"0"!=v){
            let m = cell.elem[cell.elem.selectedIndex].text;
            let row_focus = Number(currEditCell.row_focus);
            let column_focus = Number(currEditCell.column_focus);
            let cellUpdate = {
                "v": v, //内容的原始值
                "m": m, //内容的显示值
            };
            luckysheet.exitEditMode();
            luckysheet.setCellValue(row_focus,column_focus,cellUpdate);
        }
    });
});

function backPlugins(){
    $('input:checkbox').attr("checked", false);
    $('input:radio').attr("checked", false);
    layui.use(['form'], function () {
        let form = layui.form;
        form.render();
    });
}

function initDynamicPlugin(pluginsInfo,pluginServiceTypes){
    let pluginId = pluginsInfo.pluginId;
    let showName = pluginsInfo.showName;
    let needInit = pluginsInfo.needInit;
    let serviceType = pluginsInfo.serviceType;
    try{
        if(needInit!=undefined&&needInit!=null&&needInit){
            let canInit = false;
            if(serviceType!=undefined&&serviceType!=null&&serviceType.length>0){
                if(pluginServiceTypes!=undefined&&pluginServiceTypes!=null&&pluginServiceTypes.length>0){
                    if(pluginServiceTypes.indexOf(serviceType)>=0){
                        canInit = true;
                    }
                }
            }
            
            //数据库插件特定页面才需要初始化
            if(("treedbmap"==pluginId)&&canInit){
                $.ajax({
                    url: getUrl("/report/plugin/query/dbtree"),
                    type: "get",
                    data: {},
                    dataType: "json",
                    timeout: 10000,
                    headers: {'Accept': 'application/json', 'Authorization': authorization},
                    success: function (res) {
                        if(res.code==='200') {
                            let tableList = res.data;
                            if(tableList!=undefined && tableList!=null && tableList.length>0){
                                let list = [];
                                for(let i=0;i<tableList.length;i++){
                                    let columnList = tableList[i].columnList
                                    let list2 = [];
                                    for(let j = 0; j < columnList.length; j++) {
                                        list2.push({
                                            id: tableList[i].tableName + "." + columnList[j].columnName,
                                            pId: tableList[i].tableName,
                                            name: columnList[j].columnName+":" + columnList[j].columnComment
                                        });
                                    }   
                                    list.push({
                                        id: tableList[i].tableName,
                                        pId: "dbRoot",
                                        name: tableList[i].tableName + ":" + tableList[i].tableComment,
                                        children: list2
                                    });
                                }
                                
                                zDbTree = $.fn.zTree.init($("#treeDbMapId"), dbTreeSetting, list);
                            }
                        }else {
                            layer.msg(res.msg, {icon: 2});
                        }
                    },
                    error: function (data) {
                        //停止延时动画
                        var statusText = data.statusText;
                        var status = data.status;
                        if (statusText == "timeout") {
                            layer.msg("请求超时，请稍后再试！", {icon: 5});
                        } else {
                            if (status == undefined || status == null || status.length == 0) {
                                layer.msg("网络异常，请稍后再试！(-1)", {icon: 5});
                            } else {
                                layer.msg("网络异常，请稍后再试！(" + status + ")", {icon: 5});
                            }
                        }
                    }
                });
            }
        }
    }catch(error){
        console.log(showName + "初始化出错!");
    }
}

/**
 * layui树形组件--字段名转换
 * @param list
 * @returns {Array}
 */
function changeListFiled(list) {
    let newList = [];
    if (list && list.length > 0) {
        list.forEach(t => {
            let newItem = {
                title: t.value,
                id: t.code,
                children: t.children
            };
            newList.push(newItem);
            if (t.children && t.children.length > 0) {
                newItem.children = this.changeListFiled(t.children);
            }
        })
    }
    return newList
}